package edu.neu.chenhan.fragment.cooperation;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

import edu.neu.chenhan.R;
import edu.neu.chenhan.adapter.MeetingListAdapter;
import edu.neu.chenhan.listener.OnItemClickListener;
import edu.neu.chenhan.listener.OnJoinMeetingListener;
import edu.neu.chenhan.model.Meeting;
import edu.neu.chenhan.util.MockDataUtil;

public class MeetingListFragment extends Fragment {

    private RecyclerView recyclerView;
    private MeetingListAdapter adapter;
    private List<Meeting> meetingList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_meeting_list, container, false);
        recyclerView = view.findViewById(R.id.rv_meetings);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        meetingList = MockDataUtil.getMeetings();
        Log.d("MeetingListFragment", "Meeting list size: " + (meetingList != null ? meetingList.size() : 0));

        // 创建 OnItemClickListener
        OnItemClickListener itemListener = position -> {
            Log.d("MeetingListFragment", "Clicked position: " + position);
            if (meetingList != null && position >= 0 && position < meetingList.size()) {
                Meeting selectedMeeting = meetingList.get(position);
                // 跳转到 MeetingDetailFragment
                getParentFragmentManager().beginTransaction()
                        .replace(R.id.fragment_container, MeetingDetailFragment.newInstance(selectedMeeting))
                        .addToBackStack(null)
                        .commit();
            } else {
                Log.e("MeetingListFragment", "Invalid position or empty meeting list");
            }
        };

        // 创建参会按钮点击监听器
        OnJoinMeetingListener joinListener = position -> {
            Log.d("MeetingListFragment", "Join meeting clicked: " + position);
            if (meetingList != null && position >= 0 && position < meetingList.size()) {
                Meeting meeting = meetingList.get(position);
                // 跳转到参会信息填写界面
                navigateToMeetingJoinForm(meeting);
            }
        };

        // 将两个监听器传递给适配器
        adapter = new MeetingListAdapter(requireContext(), meetingList, itemListener, joinListener);
        recyclerView.setAdapter(adapter);

        return view;
    }

    // 跳转到参会信息填写界面的方法（移到了 onCreateView 外部）
    private void navigateToMeetingJoinForm(Meeting meeting) {
        MeetingJoinFormFragment formFragment = MeetingJoinFormFragment.newInstance(meeting);
        getParentFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, formFragment)
                .addToBackStack("meeting_join_form")
                .commit();
    }
}